package com.qpf.mycms.api.util;

import com.qpf.mycms.api.exception.ServiceException;
import com.qpf.mycms.api.exception.enums.BusinessExceptionEnum;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HttpServletUtil {
    private HttpServletUtil(){}
    public static HttpServletRequest getRequest() {
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        if (requestAttributes == null) {
            throw  new ServiceException(BusinessExceptionEnum.BUSINESS_OPERATION_ERROR.getDesc(), BusinessExceptionEnum.BUSINESS_OPERATION_ERROR.getCode());
        }
        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    }
    public static HttpServletResponse getResponse() {
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        if (requestAttributes == null) {
            throw  new ServiceException(BusinessExceptionEnum.BUSINESS_OPERATION_ERROR.getDesc(), BusinessExceptionEnum.BUSINESS_OPERATION_ERROR.getCode());
        }
        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
    }
}
